//
//  ZGSCountryViewController.m
//  Tour
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ZGSCountryViewController.h"
#import "AdvanceEdit.h"
#import "ZGSHotModelTwo.h"
#import "ZGSSearchCityCell.h"
#import "ZGSSearchCountryCollectionViewCell.h"
#import "ScrollWebViewController.h"
#import "ZGSSearchViewController.h"
@interface ZGSCountryViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, aAA>
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UIView *headView;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)NSMutableArray *arrPhoto;
@property (nonatomic, retain)NSMutableArray *arrHotCity;
@property (nonatomic, retain)NSMutableArray *arrNewDiscount;
@property (nonatomic, retain)MBProgressHUD *HUD;
//@property (nonatomic, retain)

@end

@implementation ZGSCountryViewController
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.hidesBarsOnSwipe = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 10, (HEIGHT / 2 - 100) / 2 - 10);
    self.navigationController.navigationBarHidden = NO;
    self.title = self.name;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.headView = [[UIView alloc] init];
    self.headView.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 2 - 100 + 60 + HEIGHT / 2 - 100 + 40 + 80);
    self.headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //    [self.tableView addSubview:self.headView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - self.navigationController.navigationBar.frame.size.height) style:UITableViewStylePlain];
    [self.tableView registerClass:[ZGSSearchCityCell class] forCellReuseIdentifier:@"tableReuseo"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.headView;
    self.tableView.rowHeight = 150;
    [self.view addSubview:self.tableView];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 2 - 100)];
    [self.headView addSubview:self.scrollView];
    
    UIButton *labelInspetMore = [UIButton buttonWithType:UIButtonTypeSystem];
    labelInspetMore.frame = CGRectMake(0, HEIGHT / 2 - 100 + 10, WIDTH, 40);
    
    [labelInspetMore setTitle:@"精彩当地游" forState:UIControlStateNormal];
//    [labelInspetMore addTarget:self action:@selector(moreCityAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.headView addSubview:labelInspetMore];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 10, (HEIGHT / 2 - 100) / 2 - 10 + 40);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 2 - 100 + 60, WIDTH, HEIGHT / 2 - 100 + 80) collectionViewLayout:flowLayout];
    //    self.collectionView.backgroundColor = [UIColor cyanColor];
    [self.collectionView registerClass:[ZGSSearchCountryCollectionViewCell class] forCellWithReuseIdentifier:@"reuseo"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.headView addSubview:self.collectionView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, HEIGHT / 2 - 100 + 60 + HEIGHT / 2 - 100 + 80, WIDTH, 40);
    [button setTitle:@"超值自由行Yeah!" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:button];
    
    
    
    
    
    self.arrPhoto = [NSMutableArray array];
    self.arrHotCity = [NSMutableArray array];
    self.arrNewDiscount = [NSMutableArray array];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.dimBackground = YES;

    [self creatDate];


    
}
- (void)creatDate{
#if 1
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88265638624203&lon=121.5393309149354&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone7%%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%%25208.4.1&v=1", self.cityid];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        
        NSArray *arrPhoto = dic[@"data"][@"photos"];
        for (NSString *str in arrPhoto) {
            
            [self.arrPhoto addObject:str];
        }
        self.arrHotCity = [ZGSHotModelTwo baseModelArr:dic[@"data"][@"local_discount"]];
        //        NSLog(@"666666%@", [self.arrHotCity[0] photo]);
        
        NSArray *arrNewDiscount = [ZGSHotModelTwo baseModelArr:dic[@"data"][@"new_discount"]];
        for (ZGSHotModelTwo *desCity in arrNewDiscount) {
            [self.arrNewDiscount addObject:desCity];
        }
        //        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 2 - 100)];
        self.scrollView.contentSize = CGSizeMake(WIDTH * self.arrPhoto.count, 0);
        self.scrollView.backgroundColor = [UIColor whiteColor];
        self.scrollView.pagingEnabled = YES;
        for (NSInteger i = 0; i < self.arrPhoto.count; i++) {
            UIImageView *imageView= [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT / 2 - 100 + 100)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.arrPhoto[i]]];
            [self.scrollView addSubview:imageView];
        }
        //        [self.headView addSubview:self.scrollView];
        //        NSLog(@"%@", self.arrNewDiscount);
        self.HUD.hidden = YES;
        [self.tableView reloadData];
        
        [self.collectionView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
#endif
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrNewDiscount.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSSearchCityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableReuseo" forIndexPath:indexPath];
    NSLog(@"%@", [self.arrNewDiscount[indexPath.row] price]);
    NSString *str = [[self.arrNewDiscount[indexPath.row] price] substringWithRange:NSMakeRange(4, [self.arrNewDiscount[indexPath.row] price].length - 4)];
    NSMutableString *tmp = [NSMutableString stringWithFormat:@"%@", str];
    NSString *strTmp = [tmp stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.labelTitle.text = [self.arrNewDiscount[indexPath.row] title];
    cell.labelMoney.text = strTmp;
    cell.labelPrice.text = [self.arrNewDiscount[indexPath.row] priceoff];
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:[self.arrNewDiscount[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrHotCity.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZGSSearchCountryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseo" forIndexPath:indexPath];
    [cell.imageCity sd_setImageWithURL:[NSURL URLWithString:[self.arrHotCity[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
    NSLog(@"%@", [self.arrHotCity[indexPath.row] photo]);
    cell.labelTitle.text = [self.arrHotCity[indexPath.row] title];
    //    NSLog(@"111111231312312321");
    return cell;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ScrollWebViewController *web = [[ScrollWebViewController alloc] init];
    NSString *tmp = [NSString stringWithFormat:@"http://z.qyer.com/deal/%@/", [self.arrNewDiscount[indexPath.row] idd]];
    web.urlStr = tmp;
    [self presentViewController:web animated:YES completion:^{
        
        
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ScrollWebViewController *web = [[ScrollWebViewController alloc] init];
    NSString *tmp = [NSString stringWithFormat:@"http://z.qyer.com/deal/%@/", [self.arrHotCity[indexPath.row] idd]];
    web.urlStr = tmp;
    [self presentViewController:web animated:YES completion:^{
        
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
